package com.tomcat.myapplication.http;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class HttpSubscribe {
    /**
     * 网络订阅者
     * Ui线程
     */
    public <T> void toSubscribeMainThread(Observable<T> observable, Subscriber<T> subscriber) {
        observable.subscribeOn(Schedulers.newThread())
                .unsubscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    /**
     * 用来统一处理Http的resultCode,并将HttpResult的Data部分剥离出来返回给subscriber
     * @param <T> Subscriber真正需要的数据类型，也就是Data部分的数据类型
     */
    public class HttpStatusFuncData<T> implements Func1<HttpStatusData<T> , T> {

        @Override
        public T call(HttpStatusData<T> tHttpStatusData) {
            return tHttpStatusData.getData().get(0);
        }
    }
    /**
     * 用来统一处理Http的resultCode,并将HttpResult的Data部分剥离出来返回给subscriber
     * @param <T> Subscriber真正需要的数据类型，也就是Data部分的数据类型
     */
    public class HttpCodeFuncData<T> implements Func1<HttpCodeData<T> ,T>{

        @Override
        public T call(HttpCodeData<T> tHttpStatusData) {
            return tHttpStatusData.getData();
        }
    }
}
